package com.project.pet.mapper;
import com.project.pet.dto.CommentDto;
import com.project.pet.pojo.Comment;
import org.apache.ibatis.annotations.*;

import javax.persistence.Id;
import java.util.List;


@Mapper
public interface CommentMapper extends tk.mybatis.mapper.common.Mapper<Comment>{

    @Select({
            "select comment.*,nick_name,avatar_url from comment left join wx_user ",
            "on comment.wx_user_id = wx_user.id where top_id='0' and info_id=#{infoId} order by star desc"
    })
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "wxUserId",column = "wx_user_id"),
            @Result(property = "infoId",column = "info_id"),
            @Result(property = "topId",column = "top_id"),
            @Result(property = "parentId",column = "parent_id"),
            @Result(property = "star",column = "star"),
            @Result(property = "content",column = "content"),
            @Result(property = "time",column = "time"),
            @Result(property = "nickName",column = "nick_name"),
            @Result(property = "avatarUrl",column = "avatar_url"),
            @Result(property = "comments",column = "id",
                    javaType = List.class,
                    many = @Many(select =
                            "com.project.pet.mapper.CommentMapper.selectChildComments"))
    })
    public List<CommentDto> selectComments(Integer infoId);

    @Select({
            "select comment.*,nick_name,avatar_url from comment left join wx_user ",
            "on comment.wx_user_id = wx_user.id where top_id=#{id}"
    })
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "wxUserId",column = "wx_user_id"),
            @Result(property = "infoId",column = "info_id"),
            @Result(property = "topId",column = "top_id"),
            @Result(property = "parentId",column = "parent_id"),
            @Result(property = "star",column = "star"),
            @Result(property = "content",column = "content"),
            @Result(property = "time",column = "time"),
            @Result(property = "nickName",column = "nick_name"),
            @Result(property = "avatarUrl",column = "avatar_url"),
            @Result(property = "parent",column = "parent_id",
                    javaType = CommentDto.class,
                    one = @One(select =
                            "com.project.pet.mapper.CommentMapper.selectComment"))
    })
    public List<CommentDto> selectChildComments(Integer id);


    @Select({
            "select comment.*,nick_name,avatar_url from comment left join wx_user ",
            "on comment.wx_user_id = wx_user.id where comment.id=#{id} and top_id!=0"
    })
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "wxUserId",column = "wx_user_id"),
            @Result(property = "infoId",column = "info_id"),
            @Result(property = "topId",column = "top_id"),
            @Result(property = "parentId",column = "parent_id"),
            @Result(property = "star",column = "star"),
            @Result(property = "content",column = "content"),
            @Result(property = "time",column = "time"),
            @Result(property = "nickName",column = "nick_name"),
            @Result(property = "avatarUrl",column = "avatar_url"),
    })
    public CommentDto selectComment(Integer id);


    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "wxUserId",column = "wx_user_id"),
            @Result(property = "infoId",column = "info_id"),
            @Result(property = "topId",column = "top_id"),
            @Result(property = "parentId",column = "parent_id"),
            @Result(property = "star",column = "star"),
            @Result(property = "content",column = "content"),
            @Result(property = "time",column = "time"),
            @Result(property = "nickName",column = "nick_name"),
            @Result(property = "avatarUrl",column = "avatar_url"),
            @Result(property = "parent",column = "parent_id",
                    javaType = CommentDto.class,
                    one = @One(select =
                            "com.project.pet.mapper.CommentMapper.selectParent"))
    })
    @Select({
            "SELECT tmp.*,wx_user.nick_name,wx_user.avatar_url from ",
            "(select * from `comment` where parent_id in (select id from `comment` where wx_user_id =1) ) tmp ",
            "left join wx_user on `tmp`.wx_user_id = wx_user.id"
    })
    public List<CommentDto> selectReply(Integer wxUserId);


    @Select({
            "select comment.*,nick_name,avatar_url from comment left join wx_user ",
            "on comment.wx_user_id = wx_user.id where comment.id=#{id}"
    })
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "wxUserId",column = "wx_user_id"),
            @Result(property = "infoId",column = "info_id"),
            @Result(property = "topId",column = "top_id"),
            @Result(property = "parentId",column = "parent_id"),
            @Result(property = "star",column = "star"),
            @Result(property = "content",column = "content"),
            @Result(property = "time",column = "time"),
            @Result(property = "nickName",column = "nick_name"),
            @Result(property = "avatarUrl",column = "avatar_url"),
    })
    public CommentDto selectParent(Integer id);
}
